﻿using Newtonsoft.Json;
using System;
using System.IO;

namespace VisionCore
{
    public static class HoldDAL
    {
        static string FileDir = Environment.CurrentDirectory + string.Format(@"\Parameter\{0}\", "HoldData");
        public static void ChangeFile(string s)
        {
            FileDir = s + string.Format(@"\Parameter\{0}\", "HoldData");
        }



        public static void Save<T>(T var, string name)
        {
            if (var == null) return;
            CreateDir();
            string tData = JsonConvert.SerializeObject(var, Formatting.Indented);
            File.WriteAllText(FileDir + "\\" + name, tData);
        }

        public static T Load<T>(T tT, string name)
        {
            if (!Exists(name))
            {
                return tT;
            }
            string tData = File.ReadAllText(FileDir + "\\" + name);
            tT = JsonConvert.DeserializeObject<T>(tData);
            return tT;
        }


        private static void CreateDir()
        {
            if (!Directory.Exists(FileDir + "\\"))
            {
                Directory.CreateDirectory(FileDir + "\\");
            }
        }

        private static bool Exists(string vRecipe)
        {
            string path = FileDir + vRecipe;
            if (!Directory.Exists(FileDir)) return false;
            if (!File.Exists(path)) return false;
            return true;
        }
    }
}
